﻿package com.dg.gaming.api.common
{
	import com.dg.gaming.api.common.msg.*;
	import com.ericfeminella.collections.*;
	import nanoxml.XMLElement;

	/**
	 * ...
	 * @author
	 */
	public class UserList extends Observable implements ITransmittable
	{
		/** List of users. */
		protected var userlist:HashMap;

		/**
		 * Default constructor of a UserList object.
		 */
		public function UserList()
		{
			userlist=new HashMap;
		}

		public function initXml(message:XMLElement):void
		{
			if (message.name != Msg.USER_LIST)
			{
				throw new TransmissionException("Error parsing UserList");
			}

			// Create new table from string and add to Hash
			var e:Array=ArrayList.createArrayList(message.chileds);
			var n:int=e.length;
			for (var i:int=0; i < n; i++)
			{
				var childMessage:XMLElement=XMLElement(e[i]);

				if (childMessage.name == Msg.USER)
				{
					var user:User=new User;
					user.initXML(childMessage);
					userlist.put(user.userid, user); // insert in hash
				}
			}

		}

		/**
		 * Flatten the current list of logged on users.
		 *
		 * @see com.dg.gaming.api.common.msg.ITransmittable#flatten()
		 */

		public function flatten():XMLElement
		{
			return null;
		}

		/**
		 * Creates a new User using a username and a rating and adds to the HashMap.
		 *
		 * @param username Username of User.
		 * @param rating   Rating of this user.
		 */
		public function addUser(user:User):void
		{
			// Add to hash
			var userId:Number=user.userid;
			userlist.put(userId, user);

			// Notify listeners
			setChanged();
			notifyObservers("+U " + userId);
		}

		/**
		 * Remove user from the list.
		 *
		 * @param username
		 */
		public function removeUser(userId:Number):void
		{
			// Remove user from hash
			userlist.remove(userId);

			// Notify listeners
			setChanged();
			notifyObservers("-U " + userId);
		}

		/**
		 * Return true if the user list contains this user.
		 *
		 * @param username  Username to check for in the list.
		 * @return          True if list contains the username as a key.
		 */
		public function containsUser(userId:Number):Boolean
		{
			return userlist.containsKey(userId);
		}

		/**
		 * Returns a list of all the users.
		 *
		 * @return  List of users as Strings in a Vector.
		 */
		public function getUsers():Array
		{
			return userlist.getKeys();
		}

		/**
		 * Return the user objects.
		 *
		 * @return
		 */
		public function getUserObjects():Array
		{
			return userlist.getValues();
		}

		/**
		 * Return a user object from a username.
		 *
		 * @param username  Username to search on
		 * @return          User object if found.
		 */
		public function getUser(userId:Number):User
		{
			return userlist.getValue(userId) as User;
		}

		/**
		 * Return the number of users.
		 *
		 * @return   Number of users (between 0 and GameProperties.getMaxNumOfUsers()).
		 */
		public function size():int
		{
			return userlist.size();
		}

		/**
		 * Return String version of the userList.
		 *
		 * @see java.lang.Object#toString()
		 */
		public override function toString():String
		{
			return userlist.getKeys().toString();
		}

		/**
		 * Notify observers.
		 *
		 * @see java.util.Observable#notifyObservers()
		 */
		public function refreshObservers():void
		{
			setChanged();
			super.notifyObservers();
		}


	}

}